/**
 * Created by liuqingxia on 2016/5/24.
 */
var connectionStatus;
var threadStatus;
var commandStatus;
var processStatus;
var proxyId;
var isFlush = false;

/**
 * main入口
 */
$(document).ready(function () {

    LTS.colFormatter.optFormat = function (v, row) {
        return generateOptButtons();
    }

    connectionStatus = $("#proxyConnectionStatus-container").ltsTable({
        url: getUrlBySuffix("/tddl/proxy/listConnection"),
        templateId: 'proxyConnectionStatus-template'
    });

    threadStatus = $("#proxyThreadStatus-container").ltsTable({
        url: getUrlBySuffix("/tddl/proxy/listThreadPool"),
        templateId: 'proxyThreadStatus-template'
    });

    commandStatus = $("#proxyCommandStatus-container").ltsTable({
        url: getUrlBySuffix("/tddl/proxy/listCommand"),
        templateId: 'proxyCommandStatus-template'
    });

    processStatus = $("#proxyProcessStarus-container").ltsTable({
        url: getUrlBySuffix("/tddl/proxy/listProcessorStatus"),
        templateId: 'proxyProcessStatus-template'
    });

    proxyId = getPageProxyId();
    if (typeof proxyId != 'undefined') {
        connectionStatus.post({proxyId: proxyId}, 1);
        threadStatus.post({proxyId: proxyId}, 1);
        commandStatus.post({proxyId: proxyId}, 1);
        processStatus.post({proxyId: proxyId}, 1);
        flush();
    }

    clickSearchBtn();
    clickKillBtn();

});


/**
 * 生成操作按钮
 */
function generateOptButtons() {
    return "" +
        '<a href="javascript:;" class="kill-btn">' +
        '<span class="label label-success" style="font-size: 14px">' +
        '<i class="fa fa-bolt"></i>&nbsp;&nbsp;' +
        'KILL' +
        '</span>' +
        '</a>&nbsp;';
    ;
}

/**
 * 点击"KILL"按钮
 */
function clickKillBtn() {
    $(document).on("click", ".kill-btn", function () {
        var id = getCurrentId($(this));
        //console.log(id);
        var params = {proxyId: proxyId, connectionId: id};
        var url = getUrlBySuffix("/tddl/proxy/killConnection");
        $.postHttpSync(params, url, showJsonMsg);
    });
}

function flush() {
    isFlush = true;
    flushStatus("showStatus(connectionStatus)", 1000);
    flushStatus("showStatus(threadStatus)", 1000);
    flushStatus("showStatus(commandStatus)", 1000);
    flushStatus("showStatus(processStatus)", 1000);
}

/**
 * 获取页面上标记的ProxyId
 * @returns {string|*}
 */
function getPageProxyId() {
    var id = $("currentId").html();
    return $.trim(id);
}

/**
 * 刷新信息
 * @param time
 */
function flushStatus(func, time) {
    var intervalId;
    try {
        intervalId = self.setInterval(func, time);
    } catch (e) {
        console.log("刷新ClusterResultVO的Board时，发生异常,异常信息为：" + e.message);
        console.log(e.stack);
        if (typeof intervalId != "undefined") {
            window.clearInterval(intervalId);
        }
    }
}

/**
 * 显示连接信息
 */
function showStatus(status) {
    status.post({proxyId: proxyId}, 1);
}

function clickSearchBtn() {
    $(document).on("click", "#searchBtn", function () {
        var name = $.trim($("#name").val());
        var url = getUrlBySuffix("/tddl/proxy/findByName");
        $.postHttpSync({name: name}, url, function (json) {
            if (json && json.success && json['list'].length > 0) {
                var proxy = json['list'][0];
                var proxyId = proxy['id'];
                changeProxyId(proxyId);
                changeTitle(proxy['name'], proxy['host']);
                if (!isFlush) {
                    flush();
                }
            } else {
                swal("失败", json['message'], "error");
            }
        })
    });
}

function changeProxyId(newProxyId) {
    proxyId = newProxyId;
}

function changeTitle(name, ip) {
    var title = $("#proxyTitle");
    title.empty();
    var content = "当前代理节点&nbsp;&nbsp;&nbsp;" + "名称：" + name + "，IP：" + ip;
    title.append(content);
}


